package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.CloudErrorBean;
import com.elinkway.infinitemovies.bean.ErrorTypeBean;
import com.elinkway.infinitemovies.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhaopeipei on 2016/3/30.
 */
public class CloudErrorParser extends LVideoMobileParser<CloudErrorBean>{

    public static String SHOULD_REPORT = "shouldReport";

    public static String ERROR_DETAILS = "errorDetails";

    public static String ERROR_CODE = "errorCode";

    public static String ERRORS = "errors";

    public static String CODE = "code";

    public static String INFO = "info";

    public static String TIMES = "times";

    public static String TYPE = "type";

    public static String URL = "url";

    public static String NOKRMAL_CODE = "0";

    @Override
    public CloudErrorBean parse(JSONObject data) throws Exception {
        CloudErrorBean cloudErrorBean = new CloudErrorBean();
        if (data != null && data.has(ERROR_CODE) &&data.optString(ERROR_CODE)
                .equalsIgnoreCase(NOKRMAL_CODE)) {
            cloudErrorBean.setShouldReport(data.optString(SHOULD_REPORT));

            cloudErrorBean.setErrorCode(data.optString(ERROR_CODE));

            cloudErrorBean.setErrorDetails(data.optString(ERROR_DETAILS));

            if (data.has(ERRORS)) {
                JSONArray arr = data.getJSONArray(ERRORS);
                for (int i = 0; i< arr.length(); i++) {
                    JSONObject jsonObject = arr.getJSONObject(i);
                    ErrorTypeBean errorTypeBean = new ErrorTypeBean();
                    if (jsonObject != null) {
                        errorTypeBean.setCode(jsonObject.optString(CODE));
                        errorTypeBean.setInfo(jsonObject.optString(INFO));
                        errorTypeBean.setTimes(jsonObject.optString(TIMES));
                        errorTypeBean.setType(jsonObject.optString(TYPE));
                        errorTypeBean.setUrl(jsonObject.optString(URL));
                    }
                    cloudErrorBean.getErrorBeans().add(errorTypeBean);
                }

            }
        }
        return cloudErrorBean;
    }

    @Override
    protected JSONObject getData(String data) throws JSONException {
        // TODO Auto-generated method stub
        LogUtils.e("", "Cloud Error getData " + data);
        return super.getData(data);
    }
}
